var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.urlimg,
    data: "",
    gift_list: "",
    isborder: 0,
    points: 12,
    point: 20,
    //兑换列表
    gifts: [],
    image:{},
  },
  change: function (e) {
    console.log(e);
    var that = this;
    var index = e.currentTarget.dataset.index;
    var isborder = that.data.isborder;
    var gift_list = that.data.gift_list;
    var points = 0;

    for (var i in gift_list) {
      if (i == index) {
        points = gift_list[i].blessing_number;
      }
    }
    that.setData({
      points: points,
      isborder: e.currentTarget.dataset.index,
      isborder_id: e.currentTarget.dataset.gift_id,
      log_id: e.currentTarget.dataset.log_id,
    })
  },
  exchange: function (e) {
    console.log(e);
    var that = this;
    console.log(that.data.gift_list[that.data.isborder]);

    if (that.data.gift_list[that.data.isborder].number > 0) {
        wx.request({
          url: app.url + 'api_blessing/check_gift_log',
          data: {
            id: that.data.id,
          },
          header: {
            'content-type': 'application/json',
            'token': wx.getStorageSync('token_key')
          }, success: function (res) {
            console.log(res.data.result);
            if (res.data.result.state == 1) {
              wx.showModal({
                title: '提示',
                content: '该活动只能兑换一次奖品\n请问确定兑换该奖品？',
                success: function (res) {
                  if (res.confirm) {
                    if (that.data.data.my_blessing_number >= that.data.points) {
                      if (that.data.gift_list[that.data.isborder].coupon_id != '0') {//领取优惠劵
                        wx.request({
                          url: app.url + 'api_blessing/add_gift_save',
                          data: {
                            id: that.data.isborder_id,
                          },
                          header: {
                            'content-type': 'application/json',
                            'token': wx.getStorageSync('token_key')
                          }, success: function (res) {
                            wx.showToast({
                              title: '领取成功',
                              icon: 'success',
                              duration: 1200,
                              success: function () {
                                wx.redirectTo({
                                  url: '/pages/blessing/detail/index?id=' + that.data.id,
                                })
                              },
                            }) 
                          }
                        })
                      } else {//领取礼品
                        wx.navigateTo({
                          url: '../infortimion/index?log_id=' + that.data.log_id + '&gift_id=' + that.data.isborder_id + '&id=' + that.data.id,
                        })
                      }
                    } else {
                      wx.showToast({
                        title: '祝福语不够',
                        icon: 'loading',
                        duration: 1200
                      })
                    }
                  }
                }
              })
            } else if (res.data.result.state == 2) {
              wx.showModal({
                title: '提示',
                content: '您已兑换过奖品，请勿重复兑换',
              })
            } else if (res.data.result.state == 3) {
              wx.showModal({
                title: '提示',
                content: '您还未开启您的集福\n请先开启集福',
                success: function (res) {
                  if (res) {
                    wx.navigateBack({ delta: '' })
                  }
                }
              })
            }
          }
        })
    }else{
      wx.showModal({
        title: '提示',
        content: '抱歉，该奖品已被兑换完',
      })
    }

  },
  imageLoad: function (e) {
    var $width = e.detail.width,    //获取图片真实宽度
      $height = e.detail.height,
      ratio = $width / $height;    //图片的真实宽高比例
    var viewWidth = 718,           //设置图片显示宽度，左右留有16rpx边距
      viewHeight = 718 / ratio;    //计算的高度值
    var image = this.data.images;
    //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
    image = {
      width: viewWidth,
      height: viewHeight
    }
    console.log(image);
    this.setData({
      images: image
    })
  },
  gift: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(id);
    if (id) {
      wx.navigateTo({
        url: '../detail/index?id=' + id,
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '您还未开启您的集福\n请先开启集福',
        success: function (res) {
          if (res) {
            wx.navigateBack({ delta: '' })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var gifts = that.data.gifts;
    var isborder = that.data.isborder;
    wx.request({
      url: app.url + 'api_blessing/gift_list',
      data: {
        id: options.id,
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      }, success: function (res) {
        console.log(res);

        that.setData({
          id: options.id,
          data: res.data.result,
          gift_list: res.data.result.gift_list,
          points: res.data.result.gift_list[0].blessing_number,
          isborder_id: res.data.result.gift_list[0].id,
          log_id: res.data.result.log_id,
          goods_nav_img: res.data.result.goods_nav_img,
          likegoods: res.data.result.goods_list,
        })
      }
    })
  },
  // 猜你喜欢列表
  click_goods: function (e) {
    wx.navigateTo({
      url: '/pages/index/detail/index?id=' + e.currentTarget.id,
    })
  },
})